package initialize

import (
	"ksirGo/global"
	"ksirGo/middleware"
	"ksirGo/router"
	"net/http"

	"github.com/gin-gonic/gin"
)

func Routers() *gin.Engine {
	// gin.SetMode(gin.ReleaseMode) # 环境断言
	Router := gin.Default()
	Static := global.CONFIG.Static
	StaticFS := global.CONFIG.StaticFS

	if Static.RelativePath != "" && Static.Root != "" {
		Router.Static(Static.RelativePath, Static.Root) // 静态网站
	}
	if StaticFS.RelativePath != "" && StaticFS.FS != "" {
		Router.StaticFS(StaticFS.RelativePath, http.Dir(StaticFS.FS)) // 虚拟文件系统
	}

	Router.MaxMultipartMemory = global.CONFIG.System.MaxMultipartMemory // 文件上传最大尺寸
	Router.Use(
		// middleware.Cross(), // 跨域
		middleware.RequestLogger(),      // 错误请求日志
		middleware.RecoveryLogger(true), // 宕机日志
	) // 添加日志记录中间件
	apiGroup := Router.Group("/api")

	router.InitRouter(apiGroup)
	router.CommonGroup(apiGroup)
	router.UserGroup(apiGroup)

	return Router
}
